namespace Pp.Core.Extentions
{
    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    using Globalization;

    public static class StringExtensions
    {
        private static readonly Regex luceneSpecialCharaktersRegex =
            new Regex(@"(?<restricted>\+|\-|\&|\||\!|\(|\)|\{|\}|\[|\]|\^|\""|\~|\*|\?|\:|\\)", RegexOptions.Compiled);
        private const string LUCENE_SPECIAL_CHARACTER_TO_ESCAPED_CHARACTER = @"\${restricted}";

        public static string ClearForLuceneQuery(this string value)
        {
            return luceneSpecialCharaktersRegex.Replace(value, LUCENE_SPECIAL_CHARACTER_TO_ESCAPED_CHARACTER);
        }
        public static string Encryption(this string str)
        {
            var random = new Random();
            var x = random.Next(1, 10);
            var enrcy = str.Select(s => s + x).Select(characterNumber => char.ConvertFromUtf32(characterNumber)).Aggregate("", (current, encCharacter) => current + encCharacter);
            return enrcy + char.ConvertFromUtf32(x);
        }
        public static string DeEncryption(this string str)
        {
            var x = str[str.Length - 1];
            str = str.Remove(str.Length - 1);
            return str.Select(s => s - x).Aggregate("", (current, a) => current + char.ConvertFromUtf32(a));
        }
        public static string GetExtentionNotDot(this string str)
        {
            return str.Substring(str.LastIndexOf('.') + 1);
        }
        //public static string GetDocumentUrl(this string str)
        //{
        //    var dz = str.Split('\\');
        //    if (dz.Length < 2) return str;
        //    var filename = dz[dz.Length - 1];
        //    var projectId = dz[dz.Length - 2];
        //    return "{0},{1}".FormatWith(filename, projectId);
        //}

        public static string GetExtention(this string str)
        {
            return str.Substring(str.LastIndexOf('.'));
        }
        /// <summary>
        /// Splits the given string. If the seperator not supplied ',' used as seperator. This method trims all part after splitting and
        /// does not return empty parts in result array.
        /// </summary>
        /// <param name="original">string that to be splitted.</param>
        public static string[] SplitAndTrim(this string original)
        {
            return original.SplitAndTrim(',');
        }

        /// <param name="original">string that to be splitted.</param>
        /// <param name="seperator">An array of characters that delimit the substring in this instance.</param>
        public static string[] SplitAndTrim(this string original, params char[] seperator)
        {
            if (string.IsNullOrEmpty(original))
                return new string[0];

            var split = from piece in original.Split(',')
                        let trimmed = piece.Trim()
                        where !string.IsNullOrEmpty(trimmed)
                        select trimmed;

            return split.ToArray();
        }

        public static string FormatWith(this string value, params object[] args)
        {
            return string.Format(value, args);
        }
        public static string ToBreakSpan(this string value, int length)
        {
            return value;
        }

        static readonly char[] _seoChars = new[] { '[', ']', '^', '(', ')', '$', '+', '?', ',', '.', '*', '|', '\\', '/', '!', '\'', '"', '@', '%', ' ', '<', '>' };
        public static bool CheckSeo(this string seo)
        {
            if (seo == null) return false;
            return seo.IndexOfAny(_seoChars) == -1;
        }
        public static string ToSeo(this string seo)
        {
            return seo.Where(s => _seoChars.Where(d => d == s).Count() == 0).Aggregate("", (current, s) => current + s);
        }
        public static string ClearHtml(this string str)
        {
            return Regex.Replace(str, @"<(.|\n)*?>", string.Empty);
        }
        public static string AddPageParameter(this string str, int i)
        {
            if (str.IndexOf('?') == -1)
                return str + ("?Page=" + i);
            return str + ("&Page=" + i);
        }
        public static string ToUserTimeString(this DateTime dateTime)
        {
            var now = DateTime.UtcNow;
            return dateTime.Date == now.Date
                       ? (now.Hour == dateTime.Hour
                              ? (now.Minute == dateTime.Minute
                                     ? (now.Second - dateTime.Second + 1) + " " + Res.Label_Second_ago
                                     : (now.Minute - dateTime.Minute) + " " + Res.Label_Minute_ago)
                              : (now.Hour - dateTime.Hour) + " " + Res.Label_Hour_ago)
                       : (now.Date - dateTime.Date).Days + " " + Res.Label_Day_ago;
        }
    }
}
